{-# OPTIONS -Wall #-}

--------------------------------------------------------------------------------
-- |
-- Module      :  Majalan.Symbolic.NoteList
-- Copyright   :  (c) Stephen Tetley 2014
-- License     :  BSD3
--
-- Maintainer  :  stephen.tetley@gmail.com
-- Stability   :  unstable
-- Portability :  GHC
--
-- Simple NoteList
--
--------------------------------------------------------------------------------

module Majalan.Symbolic.NoteList
  ( 

    Note(..)
  , GenEvent
  , phrase

  ) where

import Majalan.Pitch.ET12

import Majalan.Base





-- | At some point make this parametric on pitch...
--
data Note = Note  ET12   Beat
          | Chord [ET12] Beat
          | Rest  Beat 
  deriving (Eq,Ord,Show)


-- Pitch * Duration * Amplitude
type GenEvent uctx impl = Decimal -> Decimal -> Event uctx impl
-- 


-- Potentially Base.NoteList should use Seconds rather than 
-- Beats...

phrase :: GenEvent uctx impl -> [Note] -> Advance uctx ()
phrase mf xs = mapM_ fn  xs
  where
    fn (Note p d)   = let amp = 0.9
                          pch = pitchInHertz p in 
                      durationInSeconds d >>= \dsecs ->
                      aevent_ dsecs (mf pch amp)

    fn (Chord ps d) = let amp  = 0.9
                          pchs = map pitchInHertz ps  in 
                      durationInSeconds d >>= \dsecs ->
                      mapM_ (\pch -> aeventNoWidth dsecs (mf pch amp)) pchs >>
                      advanceCursor dsecs

    fn (Rest d)     = durationInSeconds d >>= \dsecs ->
                      advanceCursor dsecs

pitchInHertz :: TPitch pch => pch -> Decimal
pitchInHertz pch = toHertz 440.0 pch

